package com.hand.wfive.loginAndVip.ui

import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.bumptech.glide.Glide
import com.hand.wfive.R
import com.hand.wfive.activity.AboutUsActivity
import com.hand.wfive.activity.PrivacyActivity
import com.hand.wfive.activity.UserInfoCollectActivity
import com.hand.wfive.ad.TTAdManagerHolder
import com.hand.wfive.adapter.LoginMineAdapter
import com.hand.wfive.base.BaseFragment
import com.hand.wfive.loginAndVip.MobileLogin
import com.hand.wfive.loginAndVip.UserManager
import com.hand.wfive.loginAndVip.VipConfig
import com.hand.wfive.loginAndVip.model.UserEvent
import com.hand.wfive.qmoor.QmoorHelper
import com.hand.wfive.util.click.singleClick
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import kotlinx.android.synthetic.main.login_activity_userinfo.vipType
import kotlinx.android.synthetic.main.login_mine.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.jetbrains.anko.support.v4.startActivity
import org.jetbrains.anko.support.v4.toast

class MineFragment : BaseFragment() {

    private var mCanStart = false
    private val mHandler = object : Handler(Looper.getMainLooper()) {
        fun start() = sendEmptyMessageDelayed(0x102, 3000)

        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (mCanStart && this@MineFragment::mAdapter.isInitialized) {
                QmoorHelper.getInstance().getUnReadCount { mAdapter.updateMsgNum(it) }
            }
            start()
        }
    }

    private lateinit var mAdapter: LoginMineAdapter

    override fun getLayoutId() = R.layout.login_mine

    override fun initKotlinWidget() {
        registerEventBus()

        showUserInfo()
        rl_user_info.singleClick { goPersonal() }
        iv_vip.singleClick { goVip() }

        val data = arrayListOf(
            R.mipmap.login_mine_personal,
            R.mipmap.icon_set_about_us,
            R.mipmap.icon_set_online_server,
            R.mipmap.icon_set_privacy_policy,
            R.mipmap.icon_set_policy_sdk_list,
            R.mipmap.icon_set_policy_user_info,
            R.mipmap.icon_set_user_agreement
        )
        if (TTAdManagerHolder.opNoticeState()) {
            if (TTAdManagerHolder.getNoticeState()) {
                data.add(R.mipmap.icon_set_notice_open)
            } else {
                data.add(R.mipmap.icon_set_notice_close)
            }
        }
        mAdapter = LoginMineAdapter(data)
        mAdapter.setOnItemClickListener { _, _, position ->
            when (mAdapter.getItem(position)) {
                R.mipmap.login_mine_personal -> goPersonal()
                R.mipmap.icon_set_about_us -> startActivity<AboutUsActivity>()
                R.mipmap.icon_set_online_server ->
                    if (UserManager.getInstance().isLogin) {
                        QmoorHelper.getInstance().initSdk()
                        mCanStart = true
                    } else LoginIndexActivity.show(mContext, false)

                R.mipmap.icon_set_privacy_policy ->
                    PrivacyActivity.showRule(mContext, PrivacyActivity.ShowPolicy)

                R.mipmap.icon_set_policy_sdk_list ->
                    PrivacyActivity.showRule(mContext, PrivacyActivity.ShowSDKInfoShareList)

                R.mipmap.icon_set_policy_user_info ->
                    UserInfoCollectActivity.show(mContext)

                R.mipmap.icon_set_user_agreement ->
                    PrivacyActivity.showRule(activity, PrivacyActivity.ShowUserAgreement)

                R.mipmap.icon_set_notice_open, R.mipmap.icon_set_notice_close ->
                    if (TTAdManagerHolder.getNoticeState()) {
                        mAdapter.setData(position, R.mipmap.icon_set_notice_close)
                        TTAdManagerHolder.updateNotice(false)
                        toast("个性化推荐已关闭")
                    } else {
                        mAdapter.setData(position, R.mipmap.icon_set_notice_open)
                        TTAdManagerHolder.updateNotice(true)
                        toast("个性化推荐已开启")
                    }
            }
        }
        recycler_mine.layoutManager = LinearLayoutManager(context)
        (recycler_mine.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        recycler_mine.adapter = mAdapter

        mHandler.start()
    }

    private fun showUserInfo() {
        if (UserManager.getInstance().isLogin) {
            val user = UserManager.getInstance().curUser
            if (UserManager.LoginType_normal == user.loginType) {
                tv_personal.text = user.username

            } else {
                tv_personal.text = user.nickName
            }
            if (user.isVip==1){
                tv_open.text="会员中心"
                tv_vip_type.text = VipConfig.getVipLableByValue(user.vipType)
                tv_to_notice.visibility=View.GONE
                tv_to_notice1.visibility=View.GONE
                Glide.with(mContext).load(R.mipmap.icon_vip).into(user_logo)
            }else{
                tv_vip_type.text="开通会员"
                tv_to_notice.visibility=View.VISIBLE
                tv_to_notice1.visibility=View.VISIBLE
                Glide.with(mContext).load(R.mipmap.login_default_head).into(user_logo)
            }
        } else {
            tv_personal.text = "登录/注册"
            // 获取本机手机号
            tv_personal.postDelayed({ MobileLogin.getInstance().initQuickLogin() }, 500)
        }
    }

    private fun goPersonal() {
        // 个人中心
        if (UserManager.getInstance().isLogin) {
            startActivity<UserActivity>()
        } else {
            LoginIndexActivity.show(context, false)
        }
    }

    private fun goVip() {
        if (UserManager.getInstance().isLogin) {
            VipCenterActivity.show(mContext)
        } else {
            LoginIndexActivity.show(context, true)
        }
    }

    private fun hasQmoorPermission() =
        XXPermissions.isGranted(mContext, Permission.RECORD_AUDIO, Permission.CAMERA) &&
                (XXPermissions.isGranted(mContext, Permission.MANAGE_EXTERNAL_STORAGE) ||
                        XXPermissions.isGranted(mContext, Permission.WRITE_EXTERNAL_STORAGE))

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun doEventMessage(event: UserEvent) {
        showUserInfo()
    }

}
